package com.gyt.manager.controller;

import com.google.common.collect.Maps;
import com.gyt.manager.common.base.BaseController;
import com.gyt.manager.common.base.response.Response;
import com.gyt.manager.domain.result.mode.City;
import com.gyt.manager.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author gyt created on 2021/5/5 21:36
 **/
@RestController
@RequestMapping("test")
@Api(tags = "test", description = "测试", value = "test")
public class TestController extends BaseController {

    @Autowired
    private TestService testService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("testThread.do")
    @ApiOperation(value = "多线程处理1w条数据时间的对比", notes = "测试")
    public Response<Map<String, Object>> testThread() {
        Long noThreadTime = testService.testExecutor(1);
        Long threadTime = testService.testExecutor(2);
        Map<String, Object> map = Maps.newHashMap();
        map.put("noThreadTime", noThreadTime + "ms");
        map.put("threadTime", threadTime + "ms");
        return Response.ok(map);
    }

    @PostMapping("importByExcel.do")
    @ApiOperation(value = "导入数据(import)", notes = "测试")
    public Response<List<String>> importByExcel(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile file) {
        try {
            List<String> result = testService.importByExcel(file.getInputStream());
            return Response.ok(result);
        } catch (Exception e) {
            logger.error("importByExcel error", e);
            return Response.error(null);
        }
    }

    @GetMapping("exportLabelToCsv.do")
    @ApiOperation(value = "导出标签数据(export)", notes = "测试")
    public ResponseEntity<byte[]> exportLabelToCsv() {
        ResponseEntity<byte[]> result = testService.exportLabelToCsv();
        return result;
    }

    @PostMapping("export.do")
    @ApiOperation(value = "导出文件", notes = "测试")
    public Response export() {
        testService.export(response);
        return Response.ok("succeed");
    }

    @GetMapping("weather/getWeather.do")
    @ApiOperation(value = "获取天气", notes = "测试")
    public Response<?> getWeather(
            @ApiParam(value = "city编号集合:'421022,421023'", required = true)
            @RequestParam(value = "cities") String cities,
            @ApiParam(value = "获取天气:all-预报；base-实时", required = true)
            @RequestParam(value = "isTimely") String isTimely) {
        //拆分接口，获取预报天气可以用缓存
        Object result = testService.getWeather(isTimely, cities);
        return Response.ok(result, null);
    }

    @GetMapping("weather/getCitiesByExcel.do")
    @ApiOperation(value = "获取city信息", notes = "测试")
    public Response<List<City>> getCitiesByExcel() {
        List<City> cities = testService.getCitiesByExcel();
        return Response.ok(cities, null);
    }

    @GetMapping("test/findImage.do")
    @ApiOperation(value = "获取图片", notes = "测试")
    public Response<?> findImage(
            @ApiParam(value = "类型,1-emailFooter;2-emailHeader", required = true)
            @RequestParam("type") int type) {
        try {
            testService.findImage(type, response);
            return Response.ok(null);
        } catch (Exception e) {
            return Response.error(null);
        }
    }

    @GetMapping("test/findLogInfo.do")
    @ApiOperation(value = "获取日志信息", notes = "测试")
    public Response<String> findLogInfo(
            @ApiParam(value = "类型,1-获取当天日志;2-关键词筛选日志")
            @RequestParam(value = "type", defaultValue = "1", required = false) int type,
            @ApiParam(value = "日志行数")
            @RequestParam(value = "lineNum", defaultValue = "1000", required = false) int lineNum,
            @ApiParam(value = "关键字筛选")
            @RequestParam(value = "keyword", required = false) String keyword) {
        try {
            String logInfo = testService.findLogInfo(type, lineNum, keyword);
            return Response.ok(logInfo);
        } catch (Exception e) {
            return Response.error(null);
        }
    }

}
